package com.tjetc.service.impl;

import com.tjetc.dao.Mapper;
import com.tjetc.domain.Adresscity;
import com.tjetc.domain.Detail;
import com.tjetc.domain.Type;
import com.tjetc.domain.TypeDetail;
import com.tjetc.repository.DetailDao;
import com.tjetc.service.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DetailServiceImpl implements DetailService {
    @Autowired
    private Mapper mapper;
    @Autowired
    private DetailDao detailDao;


    @Override
    public List<Detail> findAllDetail() {
        List<Detail> allDetail = mapper.findAllDetail();
        for (Detail detail : allDetail){
            detailDao.save(detail);
        }
        return allDetail;
    }

    @Override
    public List<TypeDetail> findAllDetail1() {
        List<Type> allType = mapper.findAllType();
        List<Detail> allDetail = mapper.findAllDetail();
        List<TypeDetail> typeDetails = new ArrayList<>();

        for (int i = 0; i < allType.size(); i++){
            List<Detail> details=new ArrayList<>();

            for (int j = 0; j <allDetail.size() ; j++){
                if (allDetail.get(j).getType().equals(allType.get(i).getName())){
                    details.add(new Detail(
                            allDetail.get(j).getId(),
                            allDetail.get(j).getName(),
                            allDetail.get(j).getPhoto(),
                            allDetail.get(j).getPrice(),
                            allDetail.get(j).getAddress(),
                            allDetail.get(j).getTime()

                    ));

                }
            }
            TypeDetail typeDetail = new TypeDetail(details, allType.get(i).getName());
            typeDetails.add(typeDetail);
        }
        return typeDetails;
    }

    @Override
    public List<Detail> lookforDetail1Byname(String name) {
      List<Detail> details =  detailDao.findByNameLike(name);
      return details;
    }

    @Override
    public List<Detail> findAllDetail2() {
        List<Detail> allDetail = mapper.findAllDetail2();
        return allDetail;
    }

    @Override
    public List<TypeDetail>  lookforIntroduceByid3(String name) {
        List<Type> allType = mapper.findAllType();
        List<Detail> allDetail = mapper.findAllDetail();
        List<TypeDetail> typeDetails = new ArrayList<>();

        for (int i = 0; i < allType.size(); i++){
            List<Detail> details=new ArrayList<>();

            for (int j = 0; j <allDetail.size() ; j++){
                if (allDetail.get(j).getType().equals(allType.get(i).getName())){
                    details.add(new Detail(
                            allDetail.get(j).getId(),
                            allDetail.get(j).getName(),
                            allDetail.get(j).getPhoto(),
                            allDetail.get(j).getPrice(),
                            allDetail.get(j).getAddress(),
                            allDetail.get(j).getTime()

                    ));

                }
            }
            TypeDetail typeDetail = new TypeDetail(details, allType.get(i).getName());
            typeDetails.add(typeDetail);
        }


        List<TypeDetail> allTypedetali = new ArrayList<>();
        List<Adresscity> adresscityList = mapper.findcityaddress(name);
        for (int i = 0; i < adresscityList.size(); i++){
            for (TypeDetail typeDetail : typeDetails){
                for (Detail detail : typeDetail.getDetail()){
                    if (detail.getAddress().equals(adresscityList.get(i).getAddressname())){
                        allTypedetali.add(typeDetail);
                    }
                }
            }
        }
        return allTypedetali;
    }


}
